package com.itheima.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.domain.db.Questionnaire;
import com.itheima.domain.db.QuestionnaireResult;
import com.itheima.domain.db.SoulQuestion;
import com.itheima.domain.db.SoulQuestionOption;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.ReportWebVo;
import com.itheima.domain.vo.SoulQuestionOptionWebVo;
import com.itheima.service.db.QuestionnaireResultService;
import com.itheima.service.db.QuestionnaireService;
import com.itheima.service.db.SoulQuestionOptionService;
import com.itheima.service.db.SoulQuestionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SoulTestManager {

    @Reference
    private SoulQuestionOptionService soulQuestionOptionService;

    @Reference
    private SoulQuestionService soulQuestionService;

    @Reference
    private QuestionnaireService questionnaireService;

    //显示选项数据
    public PageBeanVo findOptionList(Integer page, Integer pagesize) {
        List<SoulQuestionOptionWebVo> list = new ArrayList<>();
        PageBeanVo pageBeanVo = soulQuestionOptionService.findOptionList(page,pagesize);
        List<SoulQuestionOption> soulQuestionOptionList = (List<SoulQuestionOption>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(soulQuestionOptionList)) {
            for (SoulQuestionOption soulQuestionOption : soulQuestionOptionList) {
                SoulQuestionOptionWebVo soulQuestionOptionWebVo = new SoulQuestionOptionWebVo();
                SoulQuestion question = soulQuestionService.findByQuestionId(soulQuestionOption.getQuestionId());
                if (question != null) {
                    soulQuestionOptionWebVo.setId(soulQuestionOption.getId());
                    soulQuestionOptionWebVo.setContent(soulQuestionOption.getContent());
                    soulQuestionOptionWebVo.setQuestion(question.getStem());
                    soulQuestionOptionWebVo.setScore(Integer.valueOf(soulQuestionOption.getScore()));
                   Questionnaire questionnaire = questionnaireService.findById(question.getQuestionnaireId());
                    soulQuestionOptionWebVo.setQuestionnaire(questionnaire.getName());
                    list.add(soulQuestionOptionWebVo);
                }
            }
        }
        pageBeanVo.setItems(list);
        return pageBeanVo;
    }


    //显示所有问题
    public List<SoulQuestion> findQuestionList() {
        List<SoulQuestion> soulQuestionList = soulQuestionService.findQuestionList();
        return soulQuestionList;
    }

    //保存选项
    public String saveOption(SoulQuestionOption soulQuestionOption) {
        String state = soulQuestionOptionService.save(soulQuestionOption);
        return state;
    }

    //删除选项
    public String deleteOptions(Long id) {
        soulQuestionOptionService.deleteOptions(id);
        return "ok";
    }
    //回显
    public SoulQuestionOptionWebVo showEditOption(Long id) {
       SoulQuestionOption soulQuestionOption = soulQuestionOptionService.findById(id);
        SoulQuestionOptionWebVo soulQuestionOptionWebVo = new SoulQuestionOptionWebVo();
        soulQuestionOptionWebVo.setId(soulQuestionOption.getId());
        soulQuestionOptionWebVo.setScore(Integer.valueOf(soulQuestionOption.getScore()));
        soulQuestionOptionWebVo.setQuestionId(soulQuestionOption.getQuestionId().toString());
        soulQuestionOptionWebVo.setContent(soulQuestionOption.getContent());

        return soulQuestionOptionWebVo;
    }

    //修改
    public String updateOptions(SoulQuestionOption soulQuestionOption) {
        soulQuestionOptionService.updateOptions(soulQuestionOption);
        return "ok";
    }

    @Reference
    private QuestionnaireResultService questionnaireResultService;



    //显示结果列表
    public PageBeanVo report(Integer page, Integer pagesize) {
        PageBeanVo pageBeanVo = questionnaireResultService.report(page, pagesize);

        ArrayList<ReportWebVo> reportWebVos = new ArrayList<>();
        //查找结果表
        List<QuestionnaireResult> questionnaireResultList = (List<QuestionnaireResult>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(questionnaireResultList)) {
            for (QuestionnaireResult questionnaireResult : questionnaireResultList) {
                ReportWebVo reportWebVo = new ReportWebVo();
                reportWebVo.setId(questionnaireResult.getId());
                reportWebVo.setConclusion(questionnaireResult.getContent());
                Questionnaire questionnaire = questionnaireService.findById(questionnaireResult.getQuestionnaireId());
                reportWebVo.setQuestionnaire(questionnaire.getName());
                String[] split = questionnaireResult.getScope().split(",");
                reportWebVo.setMinScore(split[0]);
                reportWebVo.setMaxScore(split[1]);
                reportWebVo.setCover(questionnaireResult.getCover());

                reportWebVos.add(reportWebVo);
            }
        }
        pageBeanVo.setItems(reportWebVos);
        return pageBeanVo;
    }

    //添加报告
    public String saveReport(ReportWebVo reportWebVo) {
        QuestionnaireResult questionnaireResult = new QuestionnaireResult();
        questionnaireResult.setQuestionnaireId(Long.valueOf(reportWebVo.getQuestionnaireId()));
        questionnaireResult.setContent(reportWebVo.getConclusion());
        questionnaireResult.setCover(reportWebVo.getCover());
        questionnaireResult.setScope(reportWebVo.getMinScore()+","+reportWebVo.getMaxScore());

        questionnaireResultService.save(questionnaireResult);
        return "ok";
    }

    //显示所有问卷
    public List<Questionnaire> findQuestionnaireList() {
        List<Questionnaire> soulQuestionList = questionnaireService.findQuestionList();
        return soulQuestionList;
    }

    @Autowired
    private OssTemplate ossTemplate;
    //上传图片
    public Map uploadReportInfo(MultipartFile file) throws IOException {
        String uploadPath = null;

        if (file != null) {
            uploadPath = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("message", "https://"+uploadPath);
        return map;
    }

    //删除报告
    public String deleteReport(Long id) {
        questionnaireResultService.deleteReport(id);
        return "ok";
    }

    //回显数据
    public ReportWebVo showEditReport(Long id) {
        QuestionnaireResult resultServiceByResult = questionnaireResultService.findByResultId(id);
        ReportWebVo reportWebVo = new ReportWebVo();
        reportWebVo.setId(resultServiceByResult.getId());
        String scope = resultServiceByResult.getScope();
        String[] split = scope.split(",");
        reportWebVo.setMaxScore(split[1]);
        reportWebVo.setMinScore(split[0]);
        Questionnaire questionnaire = questionnaireService.findById(resultServiceByResult.getQuestionnaireId());
        reportWebVo.setQuestionnaireId(resultServiceByResult.getQuestionnaireId().toString());
        reportWebVo.setQuestionnaire(questionnaire.getName());
        reportWebVo.setCover(resultServiceByResult.getCover());
        reportWebVo.setConclusion(resultServiceByResult.getContent());

        return reportWebVo;

    }

    //修改报告
    public String updateReport(ReportWebVo reportWebVo) {
        QuestionnaireResult questionnaireResult = new QuestionnaireResult();
        questionnaireResult.setId(reportWebVo.getId());
        questionnaireResult.setQuestionnaireId(Long.valueOf(reportWebVo.getQuestionnaireId()));
        questionnaireResult.setContent(reportWebVo.getConclusion());
        questionnaireResult.setCover(reportWebVo.getCover());
        questionnaireResult.setScope(reportWebVo.getMinScore()+","+reportWebVo.getMaxScore());

        questionnaireResultService.updateReport(questionnaireResult);
        return "ok";
    }
}
